package com.geostar.gmp.mina;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.mina.core.service.IoHandlerAdapter;  
import org.apache.mina.core.session.IoSession;  

import com.geostar.gmp.dao.IProcessDao;
import com.geostar.gmp.model.ProcessModel;
import com.geostar.gmp.model.factory.SessionFactory;
import com.geostar.gmp.utils.CreateID;
import com.geostar.gmp.utils.DateUtils;
  
public class MinaClientHanlder extends IoHandlerAdapter{  
      
	//需要
	private String ip ;
    @SuppressWarnings("unchecked")
	@Override  
    public void messageReceived(IoSession session, Object message)  
            throws Exception {  
    	if (message != null){
			List<ProcessModel> pmList = (List<ProcessModel>) message;
			for (ProcessModel pm : pmList) {
				pm.setId(CreateID.generate9());
				pm.setRecord_time(DateUtils.getLongTime());
				SqlSession sqlSession = SessionFactory.createSession();
				IProcessDao processDao = sqlSession.getMapper(IProcessDao.class);
				processDao.insert_pro(pm);
				sqlSession.commit();
				
				SessionFactory.closeSession(sqlSession);
			}
		}
    	session.close(true);
    }  
      
	@Override  
    public void sessionClosed(IoSession session) throws Exception {  
        session.close(true);  
    }

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		List<ProcessModel> pmList = MInitProcessXML.initProcessModel(ip);
		session.write(pmList);
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
	}  
    
    
}  
